import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_basic/pages/home/widgets/home_like.dart';
import 'package:flutter_basic/pages/home/widgets/home_nav.dart';
import 'package:flutter_basic/pages/home/widgets/home_search.dart';
import 'package:flutter_basic/pages/home/widgets/home_swipe.dart';

class Homepage extends StatefulWidget {
  const Homepage({Key? key}) : super(key: key);

  @override
  _HomepageState createState() => _HomepageState();
}

class _HomepageState extends State<Homepage> {
  Dio _dio = Dio(
    BaseOptions(baseUrl: 'http://smart-shop.itheima.net/index.php?s=/api/'),
  );

  List _swipeList = [];
  List _navList = [];
  List adList = [];
  List _goodsList = [];

  _getDate() async {
    Response res = await _dio.get('page/detail');
    List items = res.data['data']['pageData']['items'];
    setState(() {
      _swipeList = items[1]['data'];
      _navList = items[3]['data'];
      // 2.4 广告图片数据
      adList = items[4]['data'];
      _goodsList = items[6]['data'];
      print(_swipeList.toString());
    });
  }

  @override
  void initState() {
    _getDate();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '智慧商城',
          style: TextStyle(fontSize: 18, color: Colors.white),
        ),
        backgroundColor: Colors.red,
      ),
      body: ListView(
        children: [
          // 搜索框
          HomeSearch(),
          // 首页轮播
          HomeSwipe(swipeList: _swipeList),
          // 金刚区导航
          HomeNav(navList: _navList),
          // 广告位
          adList.isNotEmpty
              ? Padding(
                  padding: EdgeInsets.only(top: 5, bottom: 5),
                  child: Image.network(adList[0]['imgUrl']),
                )
              : Container(),
          Padding(
            padding: EdgeInsets.only(top: 5, bottom: 5),
            child: Container(
              alignment: Alignment.center,
              child: Text('—— 猜你喜欢 ——'),
            ),
          ),
          // 猜你喜欢商品列表
          HomeLike(goodsList: _goodsList),
        ],
      ),
    );
  }
}
